package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/11/5
 */
@Slf4j
@LiteflowComponent(value = "GoTargetPreTargetPositionWithoutWait", name = "[轮对/轮轴]前置站点-终点(不等待)")
@AllArgsConstructor
public class GoTargetPreTargetPositionWithoutWait extends NodeComponent {


    private final RobotDriverService robotDriverService;

    private final RobotMapPosService robotMapPosService;

    private final RobotService robotService;

    private final DispatchService dispatchService;

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        Robot robot = robotService.getDefaultRobot();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String targetName = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();

        //String auto = ticketsDto.getModalData().get("auto")== null ? "0":
        //        String.valueOf(ticketsDto.getModalData().get("auto"));
        //if (auto.equals("0")) {
        //    //log.info("半自动任务:");
        //    return ;
        //}

        if (targetName.equals("911017") || targetName.equals("911020")) {
            log.info("终点是缓存架/报废轴-前置点-跳过");
            return ;
        }
        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .eq(RobotMapPos::getName, targetName)
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        if (robotMapPos == null) {
           robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                    .eq(RobotMapPos::getCode, targetName)
                    .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        }
        if (targetName.equals("911012") && robotMapPos == null) {
            String reName = targetName + "-" + ticketsDto.getModalData().get("srcnum");
            log.info("新/旧轴上线区--终点" + reName);
            robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                    .eq(RobotMapPos::getName, reName)
                    .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        }

        log.info("最后找到的点位是:" + robotMapPos.getCode());
        RobotMapPos prePos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .like(RobotMapPos::getPreCode, robotMapPos.getCode())
                .eq(RobotMapPos::getMapCode, robot.getMapCode()).last(" limit 1"));
        if (prePos == null) {
            log.info("找不到前置点");
            return ;
        }
        log.info("前往终端前置目标点-不进行等待{}", prePos.getCode());
        dispatchService.go2TargetPosition(prePos, false);
        //TimeUnit.SECONDS.sleep(3);
        //IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        //while (true) {
        //    try {
        //        iRobotPosDto = robotDriverService.getWebPos();
        //        if (iRobotPosDto != null
        //                && iRobotPosDto.getCurrentStation() != null &&
        //                 prePos.getCode().equals(iRobotPosDto.getCurrentStation()) &&
        //                iRobotPosDto.isReached()) {
        //            log.info("机器人到达位置");
        //            break;
        //        }
        //        LiteFlowUtil.checkInterruptFlag();
        //        TimeUnit.SECONDS.sleep(1);
        //    }catch (FlowInterruptException e) {
        //        break;
        //    } catch (NullPointerException e) {
        //        log.error("机器人到达位置异常", e);
        //    }
        //
        //}
        //log.info("起始点机器人到达位置了");
        //iRobotPosDto = robotDriverService.getWebPos();
    }
}
